﻿using System;
using wojilu.Apps.Qycms.Domain;
using wojilu.Members.Sites.Domain;
using wojilu.Members.Users.Domain;
using wojilu.Members.Users.Interface;
using wojilu.Members.Users.Service;
using wojilu.Web.Controller.Common;
using wojilu.Web.Mvc.Attr;

namespace wojilu.Web.Controller.Admin.Qycms
{
    public class LogController : BaseController
    {
        public virtual IUserService userService { get; set; }

        public LogController()
        {
            userService = new UserService();
        }

        private void setpublic()
        {
            User user = ctx.viewer.obj as User;
            var modelPowerGroup = PowerGroup.findById(user.GroupId);
            bind("powergroup", modelPowerGroup);
        }

        [HttpGet, HttpPost]
        public virtual void LogList()
        {
            SetPublic();
            DataPage<SiteLog> list = null;
            var condition = String.Empty;
            var logusername = ctx.Get("logusername");
            var loguser = ctx.Get("loguser");
            Int32 maxperpage = ctx.PostInt("MaxPerPage");
            Int32 page_id = ctx.PostInt("page_id") + 1;
            var name = logusername;
            if (logusername.StrIsNullOrEmpty())
            {
                name = loguser;
            }
            User u = userService.GetByName(name);
            if (u == null)
            {
                list = new DataPage<SiteLog>();
                bind("list", list.Results);
                return;
            }
            condition = " UserId = " + u.Id + " order by id desc";
            CurrentRequest.setCurrentPage(page_id);
            list = SiteLog.findPage(condition, maxperpage);
            if (ctx.web.postValueAll().Count == 0)
            {
                ctx.web.ResponseWrite(list.RecordCount.ToString());
                ctx.web.ResponseEnd();
            }
            bind("list", list.Results);
        }

    }
}
